package util;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class SessionUtil {
	
	private static HttpSession getHttpSession() {
		FacesContext context = FacesContext.getCurrentInstance();  
		HttpServletRequest request = (HttpServletRequest)context.getExternalContext().getRequest();  
		HttpSession httpSession = request.getSession(false);
		return httpSession;
	}
	
	public static void set(String key, Object val) {
		HttpSession httpSession = SessionUtil.getHttpSession();
		httpSession.setAttribute(key, val);  
	}

	public static Object get(String key) {
		HttpSession httpSession = SessionUtil.getHttpSession();
		return httpSession.getAttribute(key);  
	}
	
	public static void authenticate() {
		if(SessionUtil.get("user") == null) {
			ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
			try {
				context.redirect("login.xhtml");
			} catch (Exception e) {}
		}
	}
}
